<?php

declare(strict_types=1);

namespace App\Listener;

use App\Core\Log;
use App\Event\OrderPay;
use App\Services\AccountService;
use Hyperf\Event\Annotation\Listener;
use Psr\Container\ContainerInterface;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Di\Annotation\Inject;

/**
 * @Listener
 */
class OrderPayListener implements ListenerInterface
{
    /**
     * @Inject
     * @var AccountService
     */
    private $account_service;
    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function listen(): array
    {
        return [
            OrderPay::class,
        ];
    }

    public function process(object $event)
    {
        $order = $event->order;
        $params = $event->params;
        Log::get('order')->info("编号为:".$order->id."订单分销开始");
        $result = $this->account_service->commission($order,$params);
        if(!$result['success']){
            Log::get('order')->info("编号为:".$order->id."订单分销,commission:".$result['msg']);
        }
        Log::get('order')->info("编号为:".$order->id."订单分销结束");
        if($order->replace_user_id&&$order->user_id!=$order->replace_user_id){
            Log::get('order')->info("编号为:".$order->id."订单代付开始赠送次数");
            $this->account_service->addSearchNums($order->user_id,5,'朋友代付'.$order->id);
            $this->account_service->addSearchNums($order->replace_user_id,5,'替朋友代付'.$order->id);
        }
    }
}
